<?php

namespace app\forms\group;

use yii\db\Exception;
use app\forms\Model;
use app\models\Group;
use yii\web\HttpException;
use app\models\Department;

/**
 * 组织部门树
 * Class GroupListForm
 * @package app\forms\group
 */
class ListForm extends Model
{

    /**
     * @param $param
     * @return array[]
     * @throws HttpException
     */
    public static function getList($param)
    {
        try {
            $groupId = \Yii::$app->user->identity->group_id;
            $groupArr = (new Group)->getTree($groupId);

            $group = [];
            $dept = new Department;
            foreach ($groupArr as $g) {
                $g['children'] = $dept->getTree($g['id']);
                $group[] = $g;
            }

            return ['group' => $group];
        } catch (Exception $e) {
            (new self())->serverError();
        }
    }
}